﻿using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using BugTrackingSystem.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin.Security;
using BugTrackingSystem.AppStart;
using BugTrackingSystem.BL.ViewModels;

namespace BugTrackingSystem.Controllers
{
    [Authorize]
    public class AccountController : Controller
    {
        private UserManager<ApplicationUser> userManager { get; set; }

        private ApplicationSignInManager signInManager { get; set; }

        public AccountController()
        {
        }

        public AccountController(UserManager<ApplicationUser> userManager, ApplicationSignInManager signInManager)
        {
            UserManager = userManager;
            SignInManager = signInManager;
        }

        public UserManager<ApplicationUser> UserManager
        {
            get
            {
                return userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                userManager = value;
            }
        }

        public ApplicationSignInManager SignInManager
        {
            get
            {
                return signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
            }
            private set
            {
                signInManager = value;
            }
        }

        //
        // GET: /Account/Login
        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            return RedirectToAction("Index", "Home");
        }

        //
        // POST: /Account/Login
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        //public async Task<ActionResult> Login(LoginViewModel model)
        //{
        //    //if (model.WindowsAuthentication == true)
        //    //{
        //    //    // login via windows authentication
        //    //    await WindowsAthentication();
        //    //}
        //    //else if (model.WindowsAuthentication == false && ModelState.IsValid == true)
        //    //{
        //    //    // login via form athentication
        //    //    await FormAthentication(model);
        //    //}
        //    //// when login is unsuccessful
        //    //TempData["Errors"] = ModelState;
        //    return RedirectToAction("Index", "Home");
        //}

        //
        // GET: /Account/Register
        [Authorize(Roles = "Admin")]
        public ActionResult Register()
        {
            var model = new RegisterViewModel();
            return View(model);
        }

        //
        // POST: /Account/Register
        //[HttpPost]
        //[Authorize(Roles = "Admin")]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> Register(RegisterViewModel model)
        //{
        //    //if (ModelState.IsValid)
        //    //{
        //    //    if (model.IsDomain == false)
        //    //    {
        //    //        // create no domain user
        //    //        var user = new ApplicationUser() { UserName = model.UserName };
        //    //        var result = await UserManager.CreateAsync(user, model.Password);
        //    //        if (result.Succeeded)
        //    //        {
        //    //            await SignInAsync(user, isPersistent: false);
        //    //            return RedirectToAction("Index", "Home");
        //    //        }
        //    //        else
        //    //        {
        //    //            AddErrors(result);
        //    //        }
        //    //    }
        //    //    else
        //    //    {
        //    //        var appUser = new ApplicationUser() { UserName = model.UserName };
        //    //        var loginInfo = new UserLoginInfo("Windows", model.UserName);
        //    //        var result = await UserManager.CreateAsync(appUser);
        //    //        if (result.Succeeded)
        //    //        {
        //    //            result = await UserManager.AddLoginAsync(appUser.Id, loginInfo);
        //    //            if (result.Succeeded)
        //    //            {
        //    //                await SignInAsync(appUser, isPersistent: false);
        //    //                return RedirectToAction("Index", "Home");
        //    //            }
        //    //        }
        //    //        AddErrors(result);
        //    //    }
        //    //}
        //    // If we got this far, something failed, redisplay form
        //    return View(model);
        //}

        //
        // POST: /Account/Disassociate
        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Disassociate(string loginProvider, string providerKey)
        {
            ManageMessageId? message = null;
            IdentityResult result = await UserManager.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey));
            if (result.Succeeded)
            {
                message = ManageMessageId.RemoveLoginSuccess;
            }
            else
            {
                message = ManageMessageId.Error;
            }
            return RedirectToAction("Manage", new { Message = message });
        }

        //
        // GET: /Account/Manage
        // [Authorize(Roles = "Admin")]
        public ActionResult Manage(ManageMessageId? message)
        {
            //ViewBag.StatusMessage =
            //                       message == ManageMessageId.ChangePasswordSuccess ? "Your password has been changed."
            //                       : message == ManageMessageId.SetPasswordSuccess ? "Your password has been set."
            //                         : message == ManageMessageId.RemoveLoginSuccess ? "The external login was removed."
            //                           : message == ManageMessageId.Error ? "An error has occurred."
            //                             : "";
            //ViewBag.HasLocalPassword = HasPassword();
            //ViewBag.ReturnUrl = Url.Action("Manage");
            return View();
        }

        //
        // POST: /Account/Manage
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //[Authorize(Roles = "Admin")]
        //public async Task<ActionResult> Manage(ManageUserViewModel model)
        //{
        //    //bool hasPassword = HasPassword();
        //    //ViewBag.HasLocalPassword = hasPassword;
        //    //ViewBag.ReturnUrl = Url.Action("Manage");
        //    //if (hasPassword)
        //    //{
        //    //    if (ModelState.IsValid)
        //    //    {
        //    //        IdentityResult result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);
        //    //        if (result.Succeeded)
        //    //        {
        //    //            return RedirectToAction("Manage", new { Message = ManageMessageId.ChangePasswordSuccess });
        //    //        }
        //    //        else
        //    //        {
        //    //            AddErrors(result);
        //    //        }
        //    //    }
        //    //}
        //    //else
        //    //{
        //    //    // User does not have a password so remove any validation errors caused by a missing OldPassword field
        //    //    ModelState state = ModelState["OldPassword"];
        //    //    if (state != null)
        //    //    {
        //    //        state.Errors.Clear();
        //    //    }

        //    //    if (ModelState.IsValid)
        //    //    {
        //    //        IdentityResult result = await UserManager.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);
        //    //        if (result.Succeeded)
        //    //        {
        //    //            return RedirectToAction("Manage", new { Message = ManageMessageId.SetPasswordSuccess });
        //    //        }
        //    //        else
        //    //        {
        //    //            AddErrors(result);
        //    //        }
        //    //    }
        //    }

        //    // If we got this far, something failed, redisplay form
        //    return View(model);
        //}

        //
        // POST: /Account/ExternalLogin
        //[HttpPost]
        //[AllowAnonymous]
        //[ValidateAntiForgeryToken]
        //public ActionResult ExternalLogin(string provider, string returnUrl)
        //{
        //    // Request a redirect to the external login provider
        //    return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
        //}

        //
        // GET: /Account/ExternalLoginCallback
        //[AllowAnonymous]
        //public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
        //{
        //    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
        //    if (loginInfo == null)
        //    {
        //        return RedirectToAction("Login");
        //    }

        //    // Sign in the user with this external login provider if the user already has a login
        //    var user = await UserManager.FindAsync(loginInfo.Login);
        //    if (user != null)
        //    {
        //        await SignInAsync(user, isPersistent: false);
        //        return RedirectToLocal(returnUrl);
        //    }
        //    else
        //    {
        //        // If the user does not have an account, then prompt the user to create an account
        //        ViewBag.ReturnUrl = returnUrl;
        //        ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
        //        return View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel { UserName = loginInfo.DefaultUserName });
        //    }
        //}

        ////
        //// POST: /Account/LinkLogin
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult LinkLogin(string provider)
        //{
        //    // Request a redirect to the external login provider to link a login for the current user
        //    return new ChallengeResult(provider, Url.Action("LinkLoginCallback", "Account"), User.Identity.GetUserId());
        //}

        ////
        //// GET: /Account/LinkLoginCallback
        //public async Task<ActionResult> LinkLoginCallback()
        //{
        //    var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync(XsrfKey, User.Identity.GetUserId());
        //    if (loginInfo == null)
        //    {
        //        return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
        //    }
        //    var result = await UserManager.AddLoginAsync(User.Identity.GetUserId(), loginInfo.Login);
        //    if (result.Succeeded)
        //    {
        //        return RedirectToAction("Manage");
        //    }
        //    return RedirectToAction("Manage", new { Message = ManageMessageId.Error });
        //}

        ////
        //// POST: /Account/ExternalLoginConfirmation
        //[HttpPost]
        //[AllowAnonymous]
        //[ValidateAntiForgeryToken]
        //public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        //{
        //    if (User.Identity.IsAuthenticated)
        //    {
        //        return RedirectToAction("Manage");
        //    }

        //    if (ModelState.IsValid)
        //    {
        //        // Get the information about the user from the external login provider
        //        var info = await AuthenticationManager.GetExternalLoginInfoAsync();
        //        if (info == null)
        //        {
        //            return View("ExternalLoginFailure");
        //        }
        //        var user = new ApplicationUser() { UserName = model.UserName };
        //        var result = await UserManager.CreateAsync(user);
        //        if (result.Succeeded)
        //        {
        //            result = await UserManager.AddLoginAsync(user.Id, info.Login);
        //            if (result.Succeeded)
        //            {
        //                await SignInAsync(user, isPersistent: false);
        //                return RedirectToLocal(returnUrl);
        //            }
        //        }
        //        AddErrors(result);
        //    }

        //    ViewBag.ReturnUrl = returnUrl;
        //    return View(model);
        //}

        //
        // POST: /Account/LogOff
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult LogOff()
        {
            AuthenticationManager.SignOut();
            return RedirectToAction("Index", "Home");
        }

        //
        // GET: /Account/ExternalLoginFailure
        [AllowAnonymous]
        public ActionResult ExternalLoginFailure()
        {
            return View();
        }

        [ChildActionOnly]
        [Authorize(Roles = "Admin")]
        public ActionResult RemoveAccountList()
        {
            var linkedAccounts = UserManager.GetLogins(User.Identity.GetUserId());
            ViewBag.ShowRemoveButton = HasPassword() || linkedAccounts.Count > 1;
            return (ActionResult)PartialView("_RemoveAccountPartial", linkedAccounts);
        }
       
        protected override void Dispose(bool disposing)
        {
            if (disposing && UserManager != null)
            {
                UserManager.Dispose();
                UserManager = null;
            }
            base.Dispose(disposing);
        }

        #region Helpers
        
        // Used for XSRF protection when adding external logins
        private const string XsrfKey = "XsrfId";
        
        private IAuthenticationManager AuthenticationManager
        {
            get
            {
                return HttpContext.GetOwinContext().Authentication;
            }
        }
        
        private async Task SignInAsync(ApplicationUser user, bool isPersistent)
        {
            AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
            var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
            AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
        }
             
        private void AddErrors(IdentityResult result)
        {
            foreach (var error in result.Errors)
            {
                ModelState.AddModelError("", error);
            }
        }
        
        private bool HasPassword()
        {
            var user = UserManager.FindById(User.Identity.GetUserId());
            if (user != null)
            {
                return user.PasswordHash != null;
            }
            return false;
        }
        
        public enum ManageMessageId
        {
            ChangePasswordSuccess,
            SetPasswordSuccess,
            RemoveLoginSuccess,
            Error
        }
        
        private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            else
            {
                return RedirectToAction("Index", "Home");
            }
        }
        
        private class ChallengeResult : HttpUnauthorizedResult
        {
            public ChallengeResult(string provider, string redirectUri) : this(provider, redirectUri, null)
            {
            }
            
            public ChallengeResult(string provider, string redirectUri, string userId)
            {
                LoginProvider = provider;
                RedirectUri = redirectUri;
                UserId = userId;
            }
            
            public string LoginProvider { get; set; }
            
            public string RedirectUri { get; set; }
            
            public string UserId { get; set; }
            
            public override void ExecuteResult(ControllerContext context)
            {
                var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
                if (UserId != null)
                {
                    properties.Dictionary[XsrfKey] = UserId;
                }
                context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
            }
        }
        //private async Task<ActionResult> FormAthentication(LoginViewModel model)
        //{
        //    var user = await UserManager.FindAsync(model.UserName, model.Password);
        //    if (user != null)
        //    {
        //        var isLockedOut = await UserManager.IsLockedOutAsync(user.Id);
        //        if (isLockedOut == true)
        //        {
        //            ModelState.AddModelError("", Index.UserLockOut);
        //        }
        //        else
        //        {
        //            user.LastLoginTime = DateTime.Now;
        //            await SignInAsync(user, false);
        //            await UserManager.UpdateAsync(user);
        //            await UserManager.ResetAccessFailedCountAsync(user.Id);
        //            return RedirectToAction("Content", "Home");
        //        }
        //    }
        //    else
        //    {
        //        ModelState.AddModelError("", Index.WrongPasswordOrUserName);
        //        user = UserManager.Users.FirstOrDefault(u => u.UserName == model.UserName);
        //        if (user != null)
        //        {
        //            await UserManager.AccessFailedAsync(user.Id);
        //        }
        //    }
        //    return null;
        //}
        
        #endregion
    }
}